// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_SALES_APP
// * CreateDate    : 2014-03-27 22:08:53
// * Creater       : CodeSmith
// * LastChangeDate: 2014-03-27 22:08:53
// * LastChanger   : 2014-03-27 22:08:53
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Sales
{
    /// <summary>
    /// SalesApp
    /// </summary>
    public partial class SalesAppBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objSalesAppEntity">Entity</param>
        /// <returns>Entity</returns>
        public static SalesAppEntity GetSalesApp(SalesAppEntity objSalesAppEntity)
        {
            return DataAccess.SelectSingle<SalesAppEntity>(objSalesAppEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objSalesAppEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertSalesApp(SalesAppEntity objSalesAppEntity)
        {
            return DataAccess.Insert(objSalesAppEntity);
        }

        public static bool InsertSalesApp(List<SalesAppEntity> objSalesAppEntityList)
        {
            return DataAccess.Insert(objSalesAppEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objSalesAppEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateSalesApp(SalesAppEntity objSalesAppEntity)
        {
            return DataAccess.Update(objSalesAppEntity);
        }

        public static bool UpdateSalesApp(List<SalesAppEntity> objSalesAppEntityList)
        {
            return DataAccess.Update<SalesAppEntity>(objSalesAppEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objSalesAppEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteSalesApp(SalesAppEntity objSalesAppEntity)
        {
            return DataAccess.Delete(objSalesAppEntity);
        }

        public static bool DeleteSalesApp(List<SalesAppEntity> objSalesAppEntityList)
        {
            return DataAccess.Delete<SalesAppEntity>(objSalesAppEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetSalesAppAll()
        {
            return DataAccess.SelectAllDataSet<SalesAppEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<SalesAppEntity> GetSalesAppAllList()
        {
            return DataAccess.SelectAll<SalesAppEntity>();
        }
        #endregion

        public static bool SaveSalesApp(SalesAppEntity salesApp, DataSet appItems, DataSet delete)
        {
            if (salesApp == null || appItems == null)
                return false;
            SalesAppEntity app = SalesAppBs.GetSalesApp(new SalesAppEntity { Id = salesApp.Id });
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                if (app == null)
                {
                    try
                    {
                        broker.BeginTransaction();
                        salesApp.CreatedDate = DateTime.Now;
                        salesApp.UpdatedDate = DateTime.Now;
                        List<SalesAppItemEntity> addItems = new List<SalesAppItemEntity>();
                        foreach (DataRow dr in appItems.Tables[0].Rows)
                        {
                            SalesAppItemEntity appItem = new SalesAppItemEntity(dr);
                            addItems.Add(appItem);
                        }
                        DataAccess.Insert(salesApp, broker);
                        DataAccess.Insert(addItems, broker);
                        broker.Commit();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        broker.RollBack();
                        return false;
                    }
                }
                else
                {
                    try
                    {
                        broker.BeginTransaction();
                        salesApp.UpdatedDate = DateTime.Now;
                        List<SalesAppItemEntity> addItems = new List<SalesAppItemEntity>();
                        List<SalesAppItemEntity> updateItems = new List<SalesAppItemEntity>();
                        List<SalesAppItemEntity> deleteItems = new List<SalesAppItemEntity>();
                        foreach (DataRow dr in appItems.Tables[0].Rows)
                        {
                            if (dr.RowState == DataRowState.Added)
                            {
                                SalesAppItemEntity appItem = new SalesAppItemEntity(dr);
                                addItems.Add(appItem);
                            }
                            if (dr.RowState == DataRowState.Modified)
                            {
                                SalesAppItemEntity appItem = new SalesAppItemEntity(dr);
                                updateItems.Add(appItem);
                            }
                        }
                        if (delete != null)
                        {
                            foreach (DataRow dr in delete.Tables[0].Rows)
                            {
                                SalesAppItemEntity appItem = new SalesAppItemEntity(dr);
                                deleteItems.Add(appItem);
                            }
                        }
                        DataAccess.Delete(deleteItems, broker);
                        DataAccess.Update(updateItems, broker);
                        DataAccess.Insert(addItems, broker);
                        DataAccess.Update(salesApp, broker);
                        broker.Commit();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        broker.RollBack();
                        return false;
                    }
                }

            }
        }

        public static List<SalesAppEntity> GetSalesAppForCustomerSummary(string companyId, string sobId,
            string customerId, DateTime createdDate, DateTime createdDateTo)
        {
            string sql = @"select * from SALES_APP app where app.CUSTOMER_ID='{0}'";
            sql = string.Format(sql, customerId);
            if (!string.IsNullOrEmpty(companyId))
            {
                sql += " and app.COMPANY_ID='{0}'";
                sql = string.Format(sql, companyId);
            }
            if (!string.IsNullOrEmpty(sobId))
            {
                sql += " and app.SOB_ID='{0}'";
                sql = string.Format(sql, sobId);
            }
            if (createdDate != null && createdDate != new DateTime(1, 1, 1))
            {
                sql += " and app.CREATED_DATE>='{0}'";
                sql = string.Format(sql, createdDate.ToString("yyyy-MM-dd HH:mm:ss"));
            }
            if (createdDateTo != null && createdDateTo != new DateTime(1, 1, 1))
            {
                sql += " and app.CREATED_DATE<='{0}'";
                sql = string.Format(sql, createdDateTo.ToString("yyyy-MM-dd HH:mm:ss"));
            }
            return DataAccess.Select<SalesAppEntity>(sql);
        }

        public static List<SalesAppEntity> GetSalesAppUnclosedForCustomerSummary(string companyId, string sobId,
            string customerId, DateTime createdDate, DateTime createdDateTo)
        {
            string sql = @"select app.* from SALES_APP app where app.STATUS!='WFS30' and app.CUSTOMER_ID='{0}'";

            sql = string.Format(sql, customerId);
            if (!string.IsNullOrEmpty(companyId))
            {
                sql += " and app.COMPANY_ID='{0}'";
                sql = string.Format(sql, companyId);
            }
            if (!string.IsNullOrEmpty(sobId))
            {
                sql += " and app.SOB_ID='{0}'";
                sql = string.Format(sql, sobId);
            }
            if (createdDate != null && createdDate != new DateTime(1, 1, 1))
            {
                sql += " and app.CREATED_DATE>='{0}'";
                sql = string.Format(sql, createdDate.ToString("yyyy-MM-dd HH:mm:ss"));
            }
            if (createdDateTo != null && createdDateTo != new DateTime(1, 1, 1))
            {
                sql += " and app.CREATED_DATE<='{0}'";
                sql = string.Format(sql, createdDateTo.ToString("yyyy-MM-dd HH:mm:ss"));
            }

            return DataAccess.Select<SalesAppEntity>(sql);
        }

    }


}

